function [res_absorption, parameters] = vacuum_cell_absorption_RbD1(varargin)
%VACUUM_CELL_ABSORPTION_RBD1 calculates the absorption spectrum of rubidium
%(Rb85 and Rb87) in a vacuum cell (without pressure broadening).
%
%   Syntax:
%     [res_absorption, parameters] = vacuum_cell_absorption_RbD1(output_filename, parameters)
%
%       *parameters* are given by name-value pairs as
%         'Name1', value1, 'Name2', value2, ...
%
%       The output *res_absorption* is the absorption cross section in [cm^2] as a function of *parameters('Frequency')*.
%
%   Paramter names and default values:
%   * 'Bx': magnetic field along x in [Tesla], (0.00001)
%   * 'By': magnetic field along y in [Tesla], (0.00000)
%   * 'Bz': magnetic field along z in [Tesla], (0.00003)
%   * 'Temperature': cell temperature in [degree Celsius], (20.0)
%   * 'Power': laser beam power in [W], (5e-6)
%   * 'Polarization': beam polarization, [1, 0] (linearly polarized)
%   * 'BeamRadius': beam radius in [m], (2e-3)
%   * 'Frequency': laser beam frequency (relative to atom transition) in [MHz], (-5e3:50:6e3)
%   * 'PumpingTime': pumping time in [micro-second], (10.0)
%   * 'Mode': option 'vacuum', 'vacuum-ground', 'vacuum-full', 'vacuum-ground-rate'

    import Condition.Coil
    import Atom.AlkaliMetal
    import Gas.Gas
    import Laser.AlkaliLaserBeam
    import CellSystem.VacuumCell

%% parse args
    code_version = CodeVersion();
    fprintf('Using code with commit id: %s\n', code_version);
    start_at = datetime('now'); disp(start_at);

%% parameters
    p = inputParser;

    isnonneg = @(x) isnumeric(x) && x > 0;
    memberTest= @(x) any(ismember({'vacuum', 'vacuum-ground', 'vacuum-full', 'vacuum-ground-rate'}, x));
    approxTest= @(x) any(ismember({'None', 'DopplerAverage', 'HighPressure'}, x));

    p.addParameter('OutputFile',  [PUMPING_PROJ_PATH '/phys/+CellSystem/@VacuumCell/VacuumCellRbD1.mat'],      @ischar);
    p.addParameter('Bx',          0.00001, @isnumeric);        % [Tesla]
    p.addParameter('By',          0.0,     @isnumeric);        % [Tesla]
    p.addParameter('Bz',          0.00003, @isnumeric);        % [Tesla]
    p.addParameter('Temperature', 20.0,    @isnumeric);        % [degree Celsius]
    p.addParameter('Power',       5e-6,    isnonneg);          % [W]
    p.addParameter('Polarization', [1, 0], @isnumeric);        % linearly polarized
    p.addParameter('BeamRadius',  2e-3,    isnonneg);          % [m]
    p.addParameter('Frequency',   -5e3:50:6e3,    @isnumeric); % [MHz]
    p.addParameter('PumpTime',    10.0,           isnonneg); % [micro-second]
    p.addParameter('Mode',       'vacuum-ground', memberTest);
    p.addParameter('Approx',     'None',          approxTest);
    p.addParameter('ExportFile', false,    @islogical);

    p.parse(varargin{:});
    parameters = p.Results;
    disp(parameters);

%% ingredients - system
    coil = { Condition.Coil('coilx', parameters.Bx ), ...
             Condition.Coil('coily', parameters.By ), ...
             Condition.Coil('coilz', parameters.Bz ) };

    rb85=AlkaliMetal('85Rb', coil);  rb87=AlkaliMetal('87Rb', coil);

    gases={  Gas(rb85, 'vapor', 'temperature', 273.15+parameters.Temperature, 'transition', Atom.Transition.D1, 'subspace',  parameters.Mode), ...
             Gas(rb87, 'vapor', 'temperature', 273.15+parameters.Temperature, 'transition', Atom.Transition.D1, 'subspace',  parameters.Mode)};

    pumpBeam=AlkaliLaserBeam(parameters.Power, rb87, Atom.Transition.D1, 0.0,...
                             [0 0 1], parameters.Polarization, parameters.BeamRadius);

    sys=VacuumCell(gases, pumpBeam, parameters.Approx);

%% core
    freqList = parameters.Frequency; t_pump = parameters.PumpTime;
    res_absorption = sys.total_absorption_cross_section(freqList, t_pump);

%% export
    finish_at = datetime('now'); disp(finish_at);

    if parameters.ExportFile
        save(parameters.OutputFile, 'start_at', 'finish_at', 'code_version', 'parameters', ...
                          'coil', 'rb85', 'rb87', 'gases', 'pumpBeam', 'sys', ...
                          'freqList', 'res_absorption');
    end
end
